/*
 * Author.java
 *
 * Created on 18 de julio de 2007, 16:24
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model;

import org.w3c.dom.Document;

/**
 *
 * @author mariano
 */
public class Author extends xml.Xmlizable{
    private String firstName;
    private String lastName;
    private String mail;
    
    /** Creates a new instance of Author */
    public Author(){
    }
    
    public Author( String firstName, String lastName, String mail )
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.mail = mail;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
    
    public xml.Coleccion toXmlColeccion()
    {
        xml.Coleccion auth = new xml.Coleccion( "author" );
        auth.addNodo( "first-name", this.firstName );
        auth.addNodo( "last-name", this.lastName );
        auth.addNodo( "mail", this.mail );
        
        return auth;
    }
    
    public static Author fromXml( String str )
    {
        xml.Item auth = xml.Item.fromXml( str );
        return parse( auth );
    }
    
    public static Author fromXml( Document doc )
    {
        xml.Item auth = xml.Item.fromXml( doc.getFirstChild() );
        return parse( auth );
    }
    
    private static Author parse( xml.Item auth )
    {
        if( !auth.isColeccion() || auth.getName() != "author" )
            return null;
        
        xml.Coleccion c = (xml.Coleccion)auth;
        
        xml.Nodo first = c.getNodo( 0, "first-name" );
        xml.Nodo last = c.getNodo( 1, "last-name" );
        xml.Nodo mail = c.getNodo( 2, "mail" );
        
        if( first == null || last == null || mail == null )
            return null;
        
        return new Author( first.getValue(), last.getValue(), mail.getValue() );
    }
    
    public static void main( String []args )
    {
        Author a = new Author( "Mariano", "Guerra", "luismarianoguerra@gmail.com" );
        System.out.println( a.toXmlString() );
        Author b = Author.fromXml( a.toXmlString() );
        b.setMail( "marianoguerra@bbs.frc.utn.edu.ar" );
        System.out.println( b.toXmlString() );
    }
    
}
